//
//  SLHomeDeviceCell.m
//  SlockApp
//
//  Created by 凯朱 on 2024/7/10.
//

#import "SLHomeDeviceCell.h"
#import "SLHomeDeviceLogCell.h"
#import <SlockApp-Swift.h>

static  NSString *const kCellId = @"SLHomeDeviceLogCell";
@interface SLHomeDeviceCell ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)NSMutableArray *dataArray;
@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, strong)CAGradientLayer *yLayer;
@property (nonatomic, strong)CAGradientLayer *rLayer;
@property (nonatomic, strong)LottieAnimationView *animation;
@end




@implementation SLHomeDeviceCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
   _yLayer = [self.topView addLandColorWithStart:KColor_O endColor:KColor_Y frame:CGRectMake(0, 0, kScreenWidth-40, 100)];
   _rLayer = [self.topView addLandColorWithStart:kHexColor(@"#DB1140") endColor:kHexColor(@"#DB1140") frame:CGRectMake(0, 0, kScreenWidth-40, 100)];
   
    _nameLbl.font = [UIFont boldSystemFontOfSize:22];
    _descLbl.font = kFont(12);
    
    [self.bgView2 addLandColorWithStart:kHexColor(@"#3B3B3D") endColor:kHexColor(@"#1E1E1F") frame:CGRectMake(0, 0, kScreenWidth-40,130)];
    
    _bgView.layer.cornerRadius = 20;
    _bgView.layer.masksToBounds = YES;
    _bgView.layer.borderColor = kHexColor(@"#292929").CGColor;
    _bgView.layer.borderWidth = 1;
    
    

    [self.midView addSubview:self.tableView];
    [self.midMaskView addPortColorWithStart:[kHexColor(@"#1E1E1F") colorWithAlphaComponent:1.0] endColor:[kHexColor(@"#3B3B3D") colorWithAlphaComponent:0] frame:CGRectMake(0, 0, kScreenWidth-40, 30)];
    [self.midView bringSubviewToFront:self.midMaskView];
    self.midMaskView.userInteractionEnabled = NO;
    
    kWeakSelf(self);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        if (weakself.btnBlock) {
            weakself.btnBlock(1, weakself.model);
        }
    }];
    _animationBgView.userInteractionEnabled = YES;
    [_animationBgView addGestureRecognizer:tap];
    _animationBgView.layer.cornerRadius = 25;
    _animationBgView.layer.masksToBounds = YES;
   
}

-(void)playUlockAnimation:(void(^)(void))finish{
    [AnimationLoader playAnimationAndStopWithAnimationView:_animation for:2 completion:finish];
}

-(void)setModel:(SLLockModel *)model{
    _model = model;
    _nameLbl.text = _model.deviceName;
    
    if ([ZCTool isOpenDebugModel]) {
        _nameLbl.text = [NSString stringWithFormat:@"%@ - %@",_model.deviceName,_model.deviceNo];
    }
    if (model.isUpdating) {
        _descLbl.text = kLocalString(@"Updating...");
        [_refreshBtn.imageView rotate360Degree:0.4];
    }else{
        _descLbl.text = [ZCTool updateTimeWith:model.syncTime];
        [_refreshBtn.imageView.layer removeAllAnimations];
    }
    
    if ([model.isHaveNewWarn isEqualToString:@"1"]) {
        _keyIcon.image = [UIImage imageNamed:@"icon_open_men_dark_red"];
        [self.topView.layer insertSublayer:_yLayer below:_rLayer];
        _animation = [AnimationLoader loadAnimationFrom:@"LockIconUnlock_R" into:self.animationView];
        
    }else{
        _keyIcon.image = [UIImage imageNamed:@"icon_open_men_dark"];
        [self.topView.layer insertSublayer:_rLayer below:_yLayer];
        _animation = [AnimationLoader loadAnimationFrom:@"LockIconUnlock_Y" into:self.animationView];
    }
    
    NSString *battery = [NSString stringWithFormat:@"%@%%",kReplace(_model.deviceElectricQty, @"-")];
    _batteryWidth.constant = [battery isEqualToString:@"100%"] ? 80 : 70;
    _battertLbl.text = battery;
    self.dataArray = (NSMutableArray *)model.mslUnlockLogList;
    _NoDataLbl.hidden = self.dataArray.count > 0;
    _keyNumLbl.text = kReplace(model.membersNum, @"-");
    _warnLbl.text = kReplace(model.warnNum, @"-");
    [self.tableView reloadData];
//    [_tableView scrollToBottomAnimated:NO];
}

- (IBAction)btnAction:(id)sender {
    
    if (sender == _keyBtn) {
        if (self.btnBlock) {
            self.btnBlock(2, self.model);
        }
    }else if (sender == _logBtn){
        if (self.btnBlock) {
            self.btnBlock(3, self.model);
        }
    }else if (sender == _refreshBtn){
        if (self.btnBlock) {
            self.btnBlock(4, self.model);
        }
    }else if (sender == _settingBtn){
        if (self.btnBlock) {
            self.btnBlock(5, self.model);
        }
    }
    
}

#pragma mark - ================<tableViewDataSource>===============
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SLHomeDeviceLogCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellId];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    SLLockLogModel *model = _dataArray[indexPath.row];
    cell.model = model;
    
    return cell;
}

#pragma mark - ================<UITableViewDelegate>===============
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 22;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 3.5;
    }
    return 0.1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UITableViewHeaderFooterView *headview = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"head"];
    if (!headview) {
        headview = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"head"];
        headview.frame = CGRectMake(0, 0, kScreenWidth, 3.5);
        headview.contentView.backgroundColor = KClearColor;
    }
    return headview;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UITableViewHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"foot"];
    if (!footerView) {
        footerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"foot"];
        footerView.frame = CGRectMake(0, 0, kScreenWidth, 0.1);
        footerView.contentView.backgroundColor = KClearColor;
    }
    return footerView;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.cellClickBlock) {
        self.cellClickBlock(_model);
    }
}

#pragma mark - ================<懒加载>===============
-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth-40, 80) style:UITableViewStyleGrouped];
        _tableView.estimatedRowHeight = 25;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:kCellId bundle:nil] forCellReuseIdentifier:kCellId];
        _tableView.backgroundColor = KClearColor;
    }
    return _tableView;
}

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

@end
